package com.kaikeba.controller;

import com.kaikeba.bean.BootStrapTableUser;
import com.kaikeba.bean.Message;
import com.kaikeba.bean.PageData;
import com.kaikeba.bean.User;
import com.kaikeba.exception.DuplicateCodeException;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.UserService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserController {

    @ResponseBody("/user/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        //返回数据
        List<Map<String, Integer>> list = UserService.console();
        PageData data = new PageData<>();
        data.setTotal(UserService.totalNum());
        data.setRows(list);
        Message msg = new Message();
        msg.setData(data);
        String s = JSONUtil.toJSON(msg);
        return s;
    }

    @ResponseBody("/user/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        // 获取参数
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<User> list = UserService.findAll(true, offset, pageNumber);
        List list2 = new ArrayList();
        for(User user:list){
            BootStrapTableUser user1 = new BootStrapTableUser();
            user1.setId(user.getId());
            user1.setNickName(user.getNickName());
            user1.setNumber(user.getNumber());
            user1.setPhoneNumber(user.getPhoneNumber());
            user1.setPassword(user.getPassword());
            user1.setRegistTime(DateFormatUtil.format(user.getRegistTime()));
            user1.setLastLoginTime(DateFormatUtil.format(user.getLastLoginTime())==null?"无记录":DateFormatUtil.format(user.getLastLoginTime()));
            list2.add(user1);
        }
        PageData pageData = new PageData();
        int totalNum = UserService.totalNum();
        pageData.setTotal(totalNum);
        pageData.setRows(list2);
        String json = JSONUtil.toJSON(pageData);
        return json;
    }

    @ResponseBody("/user/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        //获取值
        String nickName = request.getParameter("nickName");
        String phoneNumber = request.getParameter("phoneNumber");
        String password = request.getParameter("password");
        User user = new User(nickName,phoneNumber,password);
        boolean flag=false;
        try {
            flag = UserService.insert(user);
        } catch (DuplicateCodeException e) {
            e.printStackTrace();
        }
        Message msg = new Message();
        if(flag){
            //添加数据成功
            msg.setStatus(0);
            msg.setResult("添加数据成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("添加数据失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/findByNumber.do")
    public String findByNum(HttpServletRequest request, HttpServletResponse response){
        String phoneNumber = request.getParameter("phoneNumber");
        //调用service
        User user = UserService.findByNumber(phoneNumber);
        //传回参数
        Message msg = new Message();
        if(user!=null){
            msg.setStatus(0);
            msg.setData(user);
            msg.setResult("查到啦,显示如下");
        }else{
            msg.setStatus(-1);
            msg.setResult("不好意思,没查到");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/user/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //接收参数
        String phoneNumber = request.getParameter("phoneNumber");
        //执行service
        boolean flag = UserService.delete(phoneNumber);
        //返回结果
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("成功删除");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String s = JSONUtil.toJSON(msg);
        return s;

    }

    @ResponseBody("/user/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        //接收参数

        String phoneNumber = request.getParameter("phoneNumber");
        String newphoneNumber = request.getParameter("newphoneNumber");
        String nickName = request.getParameter("nickName");
        String password = request.getParameter("password");
        User u = new User(nickName,newphoneNumber,password);
        boolean flag = UserService.update(phoneNumber, u);
        Message msg = new Message();
        if(flag){
            msg.setStatus(0);
            msg.setResult("更新数据成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("更新数据失败");
        }
        String s = JSONUtil.toJSON(msg);
        return s;
    }
}
